package com.dsg.config.interceptors;

import com.dsg.Constants;
import com.dsg.OpenInterface;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Configuration
public class LoginInterceptor extends HandlerInterceptorAdapter {

    //Token失效状态值
    private static final int TOKEN_INVALID = 511;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if ((handler instanceof HandlerMethod) &&
                (AnnotationUtils.findAnnotation(((HandlerMethod) handler).getBeanType(), Controller.class) != null)) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            //根据注解判断是否屏蔽验证
            boolean isOpen = method.isAnnotationPresent(OpenInterface.class);
            if (!isOpen) {
                String token = request.getHeader(Constants.TOKEN_HEADER);

            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e)
            throws Exception {
        System.out.println("LoginInterceptor afterCompletion!");
    }

}
